<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <link type="text/css" rel="stylesheet" href="css/active4d.css">
        <link type="text/css" rel="stylesheet" href="css/main.css">
        <title>JooScript: classes</title>
    </head>
    <body>
        <h1>Classes <a class='hh' name='Classes'>⁋</a></h1><p>To make an impression of what <code>class</code> is it, let's create generic example:</p><pre class="active4d code-block">
<span class="LineComment"><span class="LineComment">//</span> class definition</span>
<span class="Storage">class</span> Example(){
    <span class="LineComment"><span class="LineComment">//</span> public property with explicit type</span>
    internal x Int <span class="Operator">=</span> <span class="Number">0</span>
    <span class="LineComment"><span class="LineComment">//</span> class constructor</span>
    <span class="Storage">function</span> <span class="FunctionName">new</span>(){
        self.<span class="NamedConstant">x</span> <span class="Operator">=</span> <span class="Number">123</span>
    }
    <span class="LineComment"><span class="LineComment">//</span> class method with explicit return type</span>
    <span class="Storage">function</span> getX Int() {
        <span class="Keyword">return</span> self.<span class="NamedConstant">x</span>
    }
    <span class="LineComment"><span class="LineComment">//</span> class method with explicit argument type</span>
    <span class="Storage">function</span> <span class="FunctionName">setX</span> (<span class="FunctionArgument">x Int</span>) {
        <span class="Keyword">return</span> self.<span class="NamedConstant">x</span> <span class="Operator">=</span> x
    }
}
<span class="LineComment"><span class="LineComment">//</span> create a new instance of the class</span>
let ex <span class="Operator">=</span> Example()
</pre><p>And then translate it into JavaScript:</p><pre class="active4d code-block">
<span class="LineComment"><span class="LineComment">//</span> Class builder. It got four arguments ...</span>
<span class="Operator">$</span>buildClass(
    <span class="LineComment"><span class="LineComment">//</span> Class name</span>
    <span class="String"><span class="String">&quot;</span>Example<span class="String">&quot;</span></span>, 
    <span class="LineComment"><span class="LineComment">//</span> List of ancestors</span>
    [],
    <span class="LineComment"><span class="LineComment">//</span> Class body</span>
    <span class="Storage">function</span>(__class__){
        <span class="LineComment"><span class="LineComment">//</span> public property with explicit type</span>
        <span class="Variable">this</span>.<span class="NamedConstant">x</span> <span class="Operator">=</span> <span class="Number">0</span>
        <span class="LineComment"><span class="LineComment">//</span> constructor</span>
        <span class="LibraryClassType">this</span>.<span class="FunctionName">__new__</span> = <span class="Storage">function</span>(<span class="FunctionArgument">self</span>) {
            self.<span class="NamedConstant">x</span> <span class="Operator">=</span> <span class="Number">123</span>
        }
        <span class="LineComment"><span class="LineComment">//</span> class method with explicit return type</span>
        <span class="Variable">this</span>.getX() <span class="Operator">=</span> <span class="Storage">function</span>(self) {
            <span class="Keyword">return</span> self.<span class="NamedConstant">x</span>
        }
        <span class="LineComment"><span class="LineComment">//</span> class method with explicit argument type</span>
        <span class="Variable">this</span>.setX() <span class="Operator">=</span> <span class="Storage">function</span>(self, x) {
            self.<span class="NamedConstant">x</span> <span class="Operator">=</span> x
        }
    }, 
    <span class="LineComment"><span class="LineComment">//</span> And class role or spec, where are declared all class props and it's configuration.</span>
    <span class="LineComment"><span class="LineComment">//</span> Idea of the spec fields is come from ECMAScript 5's property descriptors and mostly related</span>
    <span class="LineComment"><span class="LineComment">//</span> with it as opposite values. For example internal field = true is a shortcut </span>
    <span class="LineComment"><span class="LineComment">//</span> for enumerable, writable and configurable fields = false; hidden = true is </span>
    <span class="LineComment"><span class="LineComment">//</span> enumerable = false and so on ...</span>
    <span class="LineComment"><span class="LineComment">//</span> It is also required that type of a prop is defined.</span>
    {
        <span class="String"><span class="String">&quot;</span>x<span class="String">&quot;</span></span>: {
            type: Type.INT, 
            internal: <span class="BuiltInConstant">true</span>
        },
        <span class="String"><span class="String">&quot;</span>__new__<span class="String">&quot;</span></span>: {
            type: Type.FUNCTION,
            constructor: <span class="BuiltInConstant">true</span>,
            hidden: <span class="BuiltInConstant">true</span>
        },
        <span class="String"><span class="String">&quot;</span>getX<span class="String">&quot;</span></span>: {
            type: Type.FUNCTION,
            <span class="LineComment"><span class="LineComment">//</span> public field is not neccesary as class property</span>
            <span class="LineComment"><span class="LineComment">//</span> is public by default</span>
            <span class="Storage">public</span>: <span class="BuiltInConstant">true</span>,
            returnType: Type.INT
        },
        <span class="String"><span class="String">&quot;</span>setX<span class="String">&quot;</span></span>: {
            type: Type.FUNCTION,
            <span class="LineComment"><span class="LineComment">//</span> argument type is a list of argument types, except first (self)</span>
            argumentType: [Type.INT]
            returnType: Type.VOID
        }
    }
);
<span class="LineComment"><span class="LineComment">//</span> Create new instance of class Example</span>
let ex <span class="Operator">=</span> Example()
</pre><p>The classes idea is coming from Python language where class body is a function with it's own execution context.</p><p>As you see, classes just makes complex things easier to get. It's very helpful when codebase is growing like a bread on the yeast, as it help you not to be lost inside details and switch attention from fixing errors to create abstractions (a.k.a. program architecture).</p><p><strong>Bottom note</strong></p><p>Property fields such as <code>hidden</code> or <code>sealed</code> is depending on browser implementation of method <code>Object.defineProperty</code> which is not supported in older browsers such as Internet Explorer, Firefox < 4 and so on. You have to ask your users to change their browsers if you are rely on an object descriptors. Anyway, if browser is not ECMAScript 5 compliant and object descriptor is set to false, will raise runtime exception. <h2>Virtual namespaces <a class='hh' name='Virtual_namespaces'>⁋</a></h2></p><p><!-- <code>private</code> namespace makes property visible only for class where it's defined. Note that it will be  --></p>
        <hr/>
        Updated: Sat Jan 29 13:55:50 +0300 2011
    </body>
</html>
